/**
* @Title: MysqlConnecter.java
* @Package me.shibor.study.javaSE.database
* @Description: TODO
* @author shibor
* @date 2017年2月9日 下午9:17:06
* @version V1.0
*/
package me.shibor.study.javaSE.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @ClassName: MysqlConnecter
 * @Description: TODO
 * @author shibor
 * @date 2017年2月9日 下午9:17:06
 * 
 */
public class MysqlConnecter {
	// JDBC 驱动名及数据库 URL
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	static final String DB_URL = "jdbc:mysql://localhost:3306/spring";
	//static final String DB_URL = "jdbc:mysql://localhost:3306/spring?useSSL=false";

	// 数据库的用户名与密码，需要根据自己的设置
	static final String USER = "shibor";
	static final String PASS = "smart";

	public static Connection getConnection() {

		return getConnection(DB_URL, USER, PASS);
	}
	
	public static Connection getConnection(String url) {
		
		return getConnection(url, USER, PASS);
	}
	
	public static Connection getConnection(String url, String user, String password) {

		Connection conn = null;
		// 注册 JDBC 驱动
		try {
			Class.forName(JDBC_DRIVER);
			conn = DriverManager.getConnection(url, user, password);
			System.out.println("创建数据库连接...成功");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.err.println("创建数据库连接...失败");
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return conn;
	}
}
